import React, { Component } from 'react';
import RaisedButton from 'material-ui/RaisedButton';
import Drawer from 'material-ui/Drawer';
import './TotalScorePopover.css';
class TotalScorePopover extends Component {
  constructor(props) {
    super(props);

    this.state = {
      open: false
    };

  }

  handleTouchTap = () => {
    // This prevents ghost click.
    this.setState({
      open: !this.state.open
    });
  };

  handleRequestClose = () => {
    this.setState({
      open: false,
    });
  };

  render() {

    const { markScores } = this.props;

    Object.keys(markScores).map(key => {
      const item = markScores[key];
      //每一个分类考核评价的标准分
      let sumStandardScore = 0;
      //每一个分类考核评价的标准分
      let sumRealScore = 0;
      Object.keys(item).map(index => {
        if (item[index].isJudge) {
          sumStandardScore += item[index].standardScore;
          sumRealScore += parseFloat(item[index].markScore);
        }
        return null;
      });
      markScores[key].sumRealScore = sumRealScore;
      markScores[key].sumStandardScore = sumStandardScore;
      return null;
    });

    //总得分
    let totalScore = 0;
    if (Object.keys(markScores).length === 1) {
      Object.keys(markScores).map(key => {
        totalScore = parseFloat(markScores[key].sumRealScore) / parseFloat(markScores[key].sumStandardScore)
        return null;
      });
    } else {
      let nyScore = 0;
      //外业所有表的标准总分
      let totalWylStandardScore = 0;
      //外业所有表的总得分
      let totalWyRealScore = 0;
      Object.keys(markScores).map(key => {
        if (key === '内业考核') {
          nyScore = parseFloat(markScores[key].sumRealScore) / parseFloat(markScores[key].sumStandardScore)
        } else {
          totalWylStandardScore += parseFloat(markScores[key].sumStandardScore);
          totalWyRealScore += parseFloat(markScores[key].sumRealScore);
        }
        return null;
      });

      totalScore = nyScore * 0.5 + (totalWyRealScore / totalWylStandardScore) * 0.5
    }

    return (
      <div>
        <RaisedButton
          onClick={this.handleTouchTap}
          label="总分"
        />
        <Drawer containerStyle={{ top: '143px', left: '10px', width: '263px', height: '200px' }} open={this.state.open}>
          <div>

            <table className="scoreTable">
              <thead>
                <th></th>
                <th>标准分</th>
                <th>得分</th>
              </thead>
              <tbody>
              {Object.keys(markScores).map(key => {
                return (
                  <tr key={key}>
                    <td>{key}</td>
                    <td>{markScores[key].sumStandardScore}</td>
                    <td>{markScores[key].sumRealScore}</td>
                  </tr>
                )
              })}
              </tbody>
            </table>
            <span>总体评分:{Math.round(totalScore * 100)}</span>
          </div>
        </Drawer>
      </div>
    )
  }
}

export default  TotalScorePopover;